package cn.xd.base.application.service.query.impl;

import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import cn.xd.base.application.assembler.KeyValueAssembler;
import cn.xd.base.application.service.query.KeyValueQueryAppService;
import cn.xd.base.basic.constants.CacheConstants;
import cn.xd.base.basic.type.DictCode;
import cn.xd.base.basic.type.KeyValueId;
import cn.xd.base.domain.bean.response.JsonKeyValueTree;
import cn.xd.base.domain.bean.response.KeyValueDTO;
import cn.xd.base.domain.entity.KeyValue;
import cn.xd.base.domain.repository.KeyValueRepository;
import cn.xd.base.persistence.dos.KeyValueDO;
import cn.xd.base.persistence.mps.KeyValueMpService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import jakarta.annotation.Resource;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

@Service
public class KeyValueQueryAppServiceImpl implements KeyValueQueryAppService {

    @Resource
    private KeyValueMpService keyValueMpService;
    @Resource
    private KeyValueRepository keyValueRepository;

    /**
     * 获取字典列表
     *
     * @return 字典配置列表
     */
    @Override
    @Cacheable(cacheNames = CacheConstants.DICT_CACHE, key = "#type + ':Level:' + #level + ':Path:' + #path")
    public List<KeyValueDTO> getAllKeyValueList(String type, Integer level, String path) {
        List<KeyValueDO> list = keyValueMpService.lambdaQuery()
                .eq(StrUtil.isNotBlank(type), KeyValueDO::getDictCode, type)
                .eq(ObjUtil.isNotNull(level), KeyValueDO::getLevel, level)
                .likeRight(StrUtil.isNotBlank(path), KeyValueDO::getPath, path)
                .orderByDesc(KeyValueDO::getDictValue).list();
        return KeyValueAssembler.INSTANCE.fromData(list);
    }

    @Override
    public KeyValueDTO getKeyValue(String dictCode, String key) {
        KeyValueDO keyValueDO = keyValueMpService.getOneForLimitOneResult(
                Wrappers.lambdaQuery(KeyValueDO.class).eq(KeyValueDO::getDictCode, dictCode).eq(KeyValueDO::getDictKey, key)).get();
        if (ObjUtil.isNull(keyValueDO)) {
            return null;
        }
        return KeyValueAssembler.INSTANCE.fromData(keyValueDO);
    }

    @Override
    @Cacheable(value = CacheConstants.DICT_CACHE, key = "#dictCode.getValue() + ':ParentId:' + #parentId.getValue()+':MaxLevel:'+ #maxLevel+':FilterWithoutShow:'+ #filterWithoutShow")
    public List<JsonKeyValueTree> getKeyValueJsonTree(Boolean filterWithoutShow, Integer maxLevel, DictCode dictCode, KeyValueId parentId) {
        List<JsonKeyValueTree> trees = new ArrayList<>();
        List<KeyValue> keyValues = (List<KeyValue>) keyValueRepository.getKeyValuesByParentId(filterWithoutShow, dictCode, parentId).get();
        for (KeyValue keyValue : keyValues) {
            JsonKeyValueTree jsonKeyValueTree = new JsonKeyValueTree(keyValue);
            List<JsonKeyValueTree> labelListForJson = new ArrayList<>();
            //maxLevel=-1表示查询所有的level
            if (keyValue.getLevel().getValue() < maxLevel || maxLevel == -1) {
                labelListForJson = this.getKeyValueJsonTree(filterWithoutShow, maxLevel, dictCode, keyValue.getId());
            }
            if (CollectionUtils.isEmpty(labelListForJson)) {
                jsonKeyValueTree.setChildren(null);
            } else {
                jsonKeyValueTree.setChildren(labelListForJson);
            }
            trees.add(jsonKeyValueTree);
        }
        return trees;
    }

    @Override
    public KeyValueDTO getById(Long id) {
        KeyValueDO keyValueDO = keyValueMpService.getById(id);
        return KeyValueAssembler.INSTANCE.fromData(keyValueDO);
    }
}
